<template>
  <view class="goods-item" @click="goodsClickHandler">
      <!-- 商品左侧图片区域 -->
      <view class="goods-item-left">
        <radio :checked="goods.goods_state" color="#c00000" v-if="isShowRadio" @click="radioClickHandler"></radio>
        <image class="goods-image" :src="goods.goods_small_logo ||  defaultPic"></image>
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="goods-item-right">
        <!-- 商品标题 -->
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-box">
          <!-- 商品价格 -->
          <view class="goods-price">¥{{tofixed(goods.goods_price)}}</view>
          <!-- 商品数量 -->
          <view class="goods-count" v-if="isShowNumberBox">
            <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler"></uni-number-box>
          </view>
        </view>
      </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    data() {
      return {
        // 无数据时的适配图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    props: {
      goods: {
        type: Object,
        default: {}
      },
      isShowRadio: {
        type:Boolean,
        default: false
      },
      isShowNumberBox: {
        type:Boolean,
        default: false
      }
    },
    // filters: {
    //   tofixed(num){
    //     return Number(num).toFixed(2)
    //   }
    // },
    // computed: {
    //   tofixed(){
    //     return Number(this.goods.goods_price).toFixed(2)
    //   }
    // },
    methods: {
      tofixed(num){
         return Number(num).toFixed(2)
      },
      goodsClickHandler(){
        this.$emit('xyz', this.goods.goods_id)
      },
      numChangeHandler(value){
        // 调用父组件传入的方法
        this.$emit('num-change', {
          goods_id: this.goods.goods_id,
          goods_count: value
        })
      },
      radioClickHandler(e){
        // 调用父组件传入的方法
        this.$emit('radio-change', {
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      }
    }
  }
</script>

<style lang="scss">
  
  page {
    background-color: #ffffff;
  }
  
    .goods-item {
      display: flex;
      border-bottom: 1px solid #efefef;
      padding: 8rpx;
      .goods-item-left {
        display: flex;
        align-items: center;
        .goods-image {
          width: 200rpx;
          height: 200rpx;
          vertical-align: middle;
        }
      }
      .goods-item-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        
        .goods-box {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .goods-price{
            font-size: 32rpx;
            color: #c00000;
          }
          .goods-count {
            background-color: #F5F5F5;
          }
        }
        
        .goods-name {
          font-size: 24rpx;
        }

      }
    }
</style>
